<?php

namespace app\advertisement\logic;

use app\advertisement\model\Advaccount as AdvaccountModel;
use think\facade\Db;
use app\data\util\HttpUtil;
use ba\Exception;
use think\facade\Cache;
use app\common\library\Excel;

class AdvaccountCheckLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new AdvaccountModel();
    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 列表
     * @author zhangheng
     */
    public function index($params = [])
    {

        $shop_id = $params['shop_id'] ?? "";
        $advertiser_id = $params['advertiser_id'] ?? "";
        $limit = $params['limit'] ?? 10;
        $where = [];

        if (!empty($shop_id)) {
            $where[] = ['b.shop_id', '=', $shop_id];
        }
        if (!empty($advertiser_id)) {
            $where[] = ['a.advertiser_id', '=', $advertiser_id];
        }

        $list = Db::name('advaccount')
            ->field('a.advertiser_id,a.advertiser_name,a.status,c.nickname,d.name as shop_name')
            ->alias('a')
            ->join('ba_advaccount_admin b', 'b.advertiser_id = a.id', 'left')
            ->join('ba_admin c', 'b.admin_id = c.id', 'left')
            ->join('ba_shops d', 'b.shop_id = d.id', 'left')
            ->where($where)
            ->where('b.status', '=', 'normal')
            ->order('a.id desc')
            ->group('a.id')
            ->paginate($limit);
        $lists = $list->items();
        $data = [
            'list' => $lists,
            'total' => $list->total(),
        ];
        return json(DefaultResponse(200, true, '获取成功', $data));
    }
    
}
